package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.appletcoupon.CouponListVO;
import com.cskaoyan.bean.appletcoupon.CouponMylistData;
import com.cskaoyan.bean.appletcoupon.CouponMylistVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/05/07 16:38
 *
 * @author liutao
 */
@Service
public class AppletCouponServiceImpl implements AppletCouponService{

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;


    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Override
    public CouponMylistVO couponMylist(Integer id, Integer status, BaseParam baseParam) {
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        PageHelper.startPage(page, limit);

        List<CouponMylistData> couponMylistDataList = new ArrayList<>();
        // 用户id和status找到couponid,再找搭配优惠券信息
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause("add_time" + " desc");
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andStatusEqualTo(status.shortValue());
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);

        for (MarketCouponUser marketCouponUser : marketCouponUserList) {
            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
            CouponMylistData couponMylistData = new CouponMylistData(marketCouponUser.getId(), marketCouponUser.getCouponId(), marketCoupon.getName(),
                    marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getMin(), marketCoupon.getDiscount(), marketCouponUser.getStartTime(), marketCouponUser.getEndTime(), status == 0);
            couponMylistDataList.add(couponMylistData);
        }
        PageInfo<CouponMylistData> pageInfo = new PageInfo<>(couponMylistDataList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponMylistVO.data(total, pages, limit, page, couponMylistDataList);
    }

    @Override
    public CouponListVO couponList(BaseParam baseParam) {
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        PageHelper.startPage(page, limit);

        MarketCouponExample example = new MarketCouponExample();
        example.setOrderByClause("add_time" + " desc");
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(example);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(marketCouponList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponListVO.data(total, pages, limit, page, marketCouponList);
    }

    @Override
    public CouponMylistVO couponSelectlist(Integer id) {
        List<CouponMylistData> couponMylistDataList = new ArrayList<>();
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause("add_time" + " desc");
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);

        for (MarketCouponUser marketCouponUser : marketCouponUserList) {
            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
            CouponMylistData couponMylistData = new CouponMylistData(marketCouponUser.getId(), marketCouponUser.getCouponId(), marketCoupon.getName(),
                    marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getMin(), marketCoupon.getDiscount(), marketCouponUser.getStartTime(), marketCouponUser.getEndTime(), marketCouponUser.getStatus() == 0);
            couponMylistDataList.add(couponMylistData);
        }
        PageInfo<CouponMylistData> pageInfo = new PageInfo<>(couponMylistDataList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponMylistVO.data(total, pages, (int) total, pages, couponMylistDataList);

    }

    @Override
    public void couponReceive(Integer id, MarketCouponUser marketCouponUser) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
        marketCouponUser.setUserId(id);
        // 设置使用状态
        marketCouponUser.setStatus((short) 0);
        // marketCouponUser.setUsedTime();
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        marketCouponUser.setEndTime(marketCoupon.getEndTime());
        // marketCouponUser.setOrderId();
        marketCouponUser.setAddTime(new Date());
        marketCouponUser.setUpdateTime(new Date());
        marketCouponUser.setDeleted(false);
        marketCouponUserMapper.insert(marketCouponUser);

    }

    @Override
    public void couponExchange(Integer id, MarketCoupon marketCoupon) {
        // 优惠券兑换码找到优惠券
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andCodeEqualTo(marketCoupon.getCode());
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        MarketCoupon coupon = marketCoupons.get(0);

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(id);
        // 设置使用状态
        marketCouponUser.setStatus((short) 0);
        // marketCouponUser.setUsedTime();
        marketCouponUser.setStartTime(coupon.getStartTime());
        marketCouponUser.setEndTime(coupon.getEndTime());
        // marketCouponUser.setOrderId();
        marketCouponUser.setAddTime(new Date());
        marketCouponUser.setUpdateTime(new Date());
        marketCouponUser.setDeleted(false);
        marketCouponUserMapper.insert(marketCouponUser);
    }
}
